package cn.ssm.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.ssm.pojo.Grade;
import cn.ssm.service.GradeService;

@Controller
@RequestMapping("/gradeController")
public class GradeController {
	
	@Autowired
	@Qualifier("gradeServiceImpl")
	private GradeService gradeService;
	
	/*@RequestMapping("/addGrade")
	public ModelAndView addGrade(@RequestParam("gId")Integer gId,@RequestParam("gName")String gName){
//		System.out.println("addGrade");
//		System.out.println("gId:"+gId+"  gName:"+gName);
		ModelAndView mav = new ModelAndView();
//		mav.addObject("key", "key!!!");
//		mav.setViewName("gradeList");
		int num = gradeService.addGrade(new Grade(gId,gName));
		if(num > 0){
			//...
			List<Grade> gList = gradeService.findGradeAll();
			mav.addObject("gList", gList);
			mav.setViewName("gradeList");
		}else{
			mav.setViewName("jsp/addGrade");
		}
		return mav;
	}*/
	@RequestMapping("/addGrade")
	public ModelAndView addGrade(@ModelAttribute Grade grade){
		ModelAndView mav = new ModelAndView();
		int num = gradeService.addGrade(grade);
		if(num > 0){
			List<Grade> gList = gradeService.findGradeAll();
			mav.addObject("glist", gList);
			mav.setViewName("gradeList");
		}else{
			mav.setViewName("addGrade");
		}
		return mav;
	}
	
	
	@RequestMapping("/selectGrade")
	public ModelAndView selectGrade(@ModelAttribute Grade grade){
		ModelAndView mav = new ModelAndView();
			List<Grade> gList = gradeService.findGradeAll();
			if(gList!=null){
				mav.addObject("glist", gList);
				mav.setViewName("gradeList");	
			}
			else {
				mav.setViewName("login");
			}
		return mav;
	}
	
	
	@RequestMapping("/delGrade")
	public ModelAndView delGrade(Integer gradeId/*Map<String,Object> modelModel model*/){
		//System.out.println("delGrade");
		//model.put("MapKey", "mapValue");
		//model.addAttribute("modelKey", "model Value");
		ModelAndView mav = new ModelAndView();
		int num=gradeService.delGradeById(gradeId);
		if(num>0){
			List<Grade> glist=gradeService.findGradeAll();
			mav.addObject("glist",glist);
			mav.setViewName("gradeList");
		}else{
			mav.setViewName("gradeList");
		}
		return mav;
	}
	@RequestMapping("/updateGrade")
	public ModelAndView updateGrade(Integer gId,String gName){
		ModelAndView mav = new ModelAndView();
		int num=gradeService.updateGrade(new Grade(gId,gName));
		if(num>0){
			List<Grade> glist=gradeService.findGradeAll();
			mav.addObject("glist",glist);
			mav.setViewName("gradeList");
		}else{
			mav.setViewName("gradeList");
		}
		return mav;
		
		
	}

	public GradeService getGradeService() {
		return gradeService;
	}

	public void setGradeService(GradeService gradeService) {
		this.gradeService = gradeService;
	}
	
	
	
	/*
	@RequestMapping("/a")
	public String aaaa(){
		System.out.println("aaaa");
		return "redirect:/gradeController/b";
	}
	
	@RequestMapping("/b")
	public String bbbb(){
		System.out.println("bbbbb");
		return "";
	}
	*/
	
}
